package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.mapper.OrderMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/orderDel")
public class OrderDelServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String flightId = request.getParameter("flightId");
        System.out.println(flightId);
        try {
            SqlSession session = SessionUtil.getSqlSession();
            OrderMapper orderMapper = session.getMapper(OrderMapper.class);
            int row = orderMapper.delOrder(id);
            session.commit();
            session.close();
            if (row > 0) {
                request.getSession().setAttribute("order_msg", "删除成功！");
                request.getSession().setAttribute("flightId", flightId);
                request.getRequestDispatcher("getOrders").forward(request, response);
            }
        } catch (Exception e) {
            request.getSession().setAttribute("order_msg", "删除失败！");
            request.getRequestDispatcher("getOrders").forward(request, response);
        }
    }
}
